package com.helloae.chap07;

import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geometry.IGeographicCoordinateSystem;
import com.esri.arcgis.geometry.ILinearUnit;
import com.esri.arcgis.geometry.IParameter;
import com.esri.arcgis.geometry.IProjection;
import com.esri.arcgis.geometry.ProjectedCoordinateSystem;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriSRGeoCSType;
import com.esri.arcgis.geometry.esriSRParameterType;
import com.esri.arcgis.geometry.esriSRProjectionType;
import com.esri.arcgis.geometry.esriSRUnitType;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 使用pProjCoordSysEdit创建一个IDTM(Idaho Tranverse Mercator coordinate system)<br>
 * 该程序在ArcGIS 10.0 sp5下会导致JRE崩溃，从日志初步判断Geometry.dll有关
 * @author zhushijie
 */
public class IDTM_OnTheFly {

	public static void main(String[] args) throws UnknownHostException, IOException {
		// 初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//定义坐标的投影为横轴墨卡托
		//地理坐标系为NAD83
		//线性单位为米
		SpatialReferenceEnvironment pSpatRefFact = new SpatialReferenceEnvironment();
		IProjection pProjection = pSpatRefFact.createProjection(esriSRProjectionType.esriSRProjection_TransverseMercator);
		IGeographicCoordinateSystem pGCS = pSpatRefFact.createGeographicCoordinateSystem(esriSRGeoCSType.esriSRGeoCS_NAD1983);
		ILinearUnit pLinearUnit = (ILinearUnit) pSpatRefFact.createUnit(esriSRUnitType.esriSRUnit_Meter);
		
		IParameter[] aParamArray = new IParameter[5];
		//创建5个参数并赋值，包括假东、假北、中央经线、起始纬线、比例因子
		aParamArray[0] = pSpatRefFact.createParameter(esriSRParameterType.esriSRParameter_FalseEasting);
		aParamArray[1] = pSpatRefFact.createParameter(esriSRParameterType.esriSRParameter_FalseNorthing);
		aParamArray[2] = pSpatRefFact.createParameter(esriSRParameterType.esriSRParameter_CentralMeridian);
		aParamArray[3] = pSpatRefFact.createParameter(esriSRParameterType.esriSRParameter_LatitudeOfOrigin);
		aParamArray[4] = pSpatRefFact.createParameter(esriSRParameterType.esriSRParameter_ScaleFactor);
		
		aParamArray[0].setValue(2500000);
		aParamArray[1].setValue(1200000);
		aParamArray[2].setValue(-114);
		aParamArray[3].setValue(42);
		aParamArray[4].setValue(0.9996);
		
		ProjectedCoordinateSystem pProjCoordSysEdit = new ProjectedCoordinateSystem();
		pProjCoordSysEdit.define("UserDefinedPCS", "UserDefinedAlias", "UsrDefAbbrv", "Custom IDTM", "Suitable for Idaho", pGCS, pLinearUnit, pProjection, aParamArray);
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

}
